<?php
/**[文章管理控制器]
 * @Author: 976123967@qq.com
 * @Date:   2014-08-15 16:44:16
 * @Last Modified by:   chenryoku
 * @Last Modified time: 2014-11-16 11:15:12
 */
class ArticleAction extends PublicAction{
	private $categoryModel;
	public function _initialize()
	{
		parent::_initialize();
		$this->model = D('Article');
		$this->assign('flag' ,C('flag'));
		$this->categoryModel = D('Category');
	}

	/**
	 * [index 内容列表]
	 * @return [type] [description]
	 */
	public function index()
	{
		// 数据
		$data = D('ArticleView')->get_data();
		// 分页
		if($data)
		{

			$this->assign('page',$data['page']);
			unset($data['page']);
		}
		// 分配
		$this->assign('data',$data);
		$cid = I('get.cid');
		$this->assign('cname',$this->categoryModel->get_field($cid,'cname'));
		// 显示
		$this->display();
	}

	/**
	 * [add 添加]
	 */
	public function add()
	{
		
		if(IS_POST)
		{
			$cid = I('post.category_cid');
			// 检查当前栏目的cat_type
			$catType = $this->categoryModel->get_field($cid,'cat_type');
			if($catType==4)
				$url = U('Index/welcome');
			else
				$url = U('Article/index',array('cid'=>$cid));
			if(!$this->model->create()) $this->error($this->model->getError());
			if(!$this->model->add_article()) $this->error($this->model->getError());
			
			$this->success('添加内容成功',$url);
			die;
		}
		$cid = I('get.cid',null,'intval');
		$this->assign('cid',$cid);
		$this->assign('cname',$this->categoryModel->get_field($cid,'cname'));

		// 栏目分类
        $category=$this->categoryModel->get_parent_child($cid);
        $this->assign('category',$category);

		
		// 检查当前栏目的cat_type
		$catType = $this->categoryModel->get_field($cid,'cat_type');
		$this->assign('catType',$catType);
		if($catType==4)
		{
			// 检查当前是否已经有添加了内容
			if($aid = $this->model->get_article_cid($cid))
				$this->redirect("Article/edit",array('aid'=>$aid,'cid'=>$cid));
		}

		// 顶级栏目信息
		$pids = $this->categoryModel->get_parent_cid($cid);
		$pids = array_reverse($pids);
		$this->assign('topid',$pids[0]);
		$this->assign('topname',$this->categoryModel->get_field($pids[0],'cname'));
		
		// 扩展字段表单
        $fieldModel = D('Field');
   		$mid = $this->categoryModel->get_field($cid,'model_mid');
   		$extForm = $fieldModel->get_field_form($mid);
   		//p($extFrom);die;
   		$this->assign('extForm',$extForm);
   		// 验证
   		$validate = $fieldModel->get_validate($mid);
   		$this->assign('validate',$validate);




   		// 读取栏目的文档类型表单
   		$attrModel = D('Attr');
   		$typeid = $this->categoryModel->get_field($cid,'type_typeid');
   		$attrForm = $attrModel->get_attr_form($typeid);
   		$this->assign('attrForm',$attrForm);

   		// 验证文档类型中是否有关联图集的属性
   		$attrPicCount = $attrModel->check_attr_pic($typeid);
   		if($attrPicCount>1)
   			$this->error('关联图集的属性只能一个');
   		$this->assign('attrPicCount',$attrPicCount);

		$this->display();
	}

	/**
	 * [edit 编辑]
	 * @return [type] [description]
	 */
	public function edit()
	{
		$categoryModel = D('Category');
		if(IS_POST)
		{
			$cid = I('post.category_cid');
			// 检查当前栏目的cat_type
			$catType = $this->categoryModel->get_field($cid,'cat_type');
			if($catType==4)
				$url = U('Index/welcome');
			else
				$url = U('Article/index',array('cid'=>$cid));

			if(!$this->model->create()) $this->error($this->model->getError());
			if(!$this->model->save_article()) $this->error($this->model->getError());
			
			$this->success('编辑内容成功',$url);
			die;
		}


		$aid=I('get.aid');
        if(!$aid) _404('信息错误',U('Index/welcome'));

		// 栏目信息
        $cid = I('get.cid',null,'intval');
		$this->assign('cid',$cid);
		$this->assign('cname',$this->categoryModel->get_field($cid,'cname'));

		// 检查当前栏目的cat_type
		$catType = $this->categoryModel->get_field($cid,'cat_type');
		$this->assign('catType',$catType);


		// 栏目分类
        $category=$categoryModel->get_parent_child($cid);
        $this->assign('category',$category);

        // 顶级栏目信息
        $pids = $this->categoryModel->get_parent_cid($cid);
		$pids = array_reverse($pids);
		$this->assign('topid',$pids[0]);
		$this->assign('topname',$this->categoryModel->get_field($pids[0],'cname'));



        // 读取旧数据
        $db = D('ArticleView');
        $field=$db->get_article_one($aid);
        $this->assign('field',$field);


        // 扩展字段表单
        $fieldModel =D('Field');
   		$mid = $this->categoryModel->get_field($cid,'model_mid');
   		$extForm = $fieldModel->get_field_form($mid,$field);
   		$this->assign('extForm',$extForm);
        
        // 验证
   		$validate = $fieldModel->get_validate($mid);
   		$this->assign('validate',$validate);


   		// 读取栏目的文档类型表单
   		$attrModel = D('Attr');
   		$typeid = $this->categoryModel->get_field($cid,'type_typeid');
   		$attrForm = $attrModel->get_attr_form($typeid,$aid);
   		$this->assign('attrForm',$attrForm);


   		// 验证文档类型中是否有关联图集的属性
   		$attrPicCount = $attrModel->check_attr_pic($typeid);
   		if($attrPicCount>1)
   			$this->error('关联图集的属性只能一个');
   		$this->assign('attrPicCount',$attrPicCount);

   		// 所有图集
   		$picModel = D('Pic');
   		if($attrPicCount)
       		$pics = $picModel->get_all_by_attr($aid);
   		else
        	$pics = $picModel->get_all($aid);
        $this->assign('pics',$pics);


       
	

		$this->display();
	}

	/**
	 * [del 删除]
	 * @return [type] [description]
	 */
	public function del()
	{
		$aid = I('get.aid');
	
        if(!$aid) _404('信息错误',U('Index/welcome'));
        $this->model->del_article($aid);
        $cid = I('get.cid');
        $this->success('删除内容成功',U('Article/index',array('cid'=>$cid)));
	}

	public function cover()
	{
		$this->display();
	}

	/**
	 * [ajax_category 处理分类]
	 * @return [type] [description]
	 */
	public function ajax_category()
	{
		$category = array();
		$cate = $this->categoryModel->category;
		if (!empty($cate))
		{
			foreach ($cate as $n => $cat) 
			{
				$data = array();
				//过滤掉外部链接栏目
				if ($cat['cat_type'] != 3) 
				{
					//单文章栏目
					if ($cat['cat_type'] == 4) 
					{
						$link = U('Article/add',array('cid'=>$cat['cid']));
						$url = $link;
					} 
					else if ($cat['cat_type'] == 1 || $cat['cat_type'] == 2) 
					{
						$url = U('Article/index', array('cid' => $cat['cid'], 'sta' => 2));
					} 
					else 
					{
						$url = 'javascript:;';
					}
					$data['id'] = $cat['cid'];
					$data['pId'] = $cat['pid'];
					$data['url'] = $url;
					$data['target'] = 'content';
					$data['open'] = true;
					$type='';
					if($cat['cat_type']==4)
						$type='(单)';
					$data['name'] = $cat['cname'].$type;
					$category[] = $data;
				}
			}
		}
		$this -> ajaxReturn($category);
	}

	/**
	 * [update 更新信息]
	 * @return [type] [description]
	 */
	public function update()
	{
		$url=U('Article/index',array('cid'=>$_POST['cid']));
		//执行操作
		switch (true) 
		{
			case isset($_POST['order']):
				if(!$this->model->update_order())
					$this->error($this->model->getError(),$url);
				$this->success('排序成功',$url);
				break;
			case isset($_POST['check']):
				if(!$this->model->update_check(2))
					$this->error($this->model->getError(),$url);
				$this->success('审核成功',$url);
				break;
			case isset($_POST['cancel_check']):
				if(!$this->model->update_check(1))
					$this->error($this->model->getError(),$url);
				$this->success('取消审核成功',$url);
				break;
			case isset($_POST['del_check']):
				if(!$this->model->delete_check())
					$this->error($this->model->getError(),$url);
				$this->success('删除成功',$url);
				break;
			case isset($_POST['check_flag']):
				if(!$this->model->change_flag(1))
					$this->error($this->model->getError(),$url);
				$flag = I('post.flag');
				$this->success('设置'.$flag.'成功',$url);
				break;
			case isset($_POST['cancel_flag']):
				if(!$this->model->change_flag(2))
					$this->error($this->model->getError(),$url);
				$flag = I('post.flag');
				$this->success('取消'.$flag.'成功',$url);
				break;
			default:
				$this->error('动作选择错误',$url);
		}

	}


	/**
	 * [del_pic 图片删除]
	 * @return [type] [description]
	 */
	public function del_pic()
	{
		$picId=I('post.picId');
		if($picId)
		{
			D('Pic')->delete_pic($picId);
			$this->ajaxReturn(1);
		}
		else
		{
			$this->ajaxReturn(0);
		}
	}

	/**
	 * [del_pic_attr ajax删除图片集合通过属性值关联外键]
	 * @return [type] [description]
	 */
	public function del_pic_attr()
	{
		$attrValueId = I('post.attrValueId');
		D('Pic')->delete_pic_by_attrValueId($attrValueId);
	}


	/**
	 * [del_attachement ajax删除图片或者删除上传的文件]
	 * @return [type] [description]
	 */
	public function del_attachement()
	{
		if(!IS_AJAX) $this->error('错误');
		$aid = I('post.aid');
		$field =I('post.field');
		$table =I('post.table');
		$this->model->del_attachment($aid,$field,$table);
		$this->ajaxReturn(1);
	}

}